import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_aygsh/util/Toast.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/SignUtil.dart';
import '../../base/Constant.dart';
import 'package:flutter/services.dart';

class KFPage extends StatefulWidget {
  KFPage({Key key}) : super(key: key);

  @override
  _KFPageState createState() => _KFPageState();
}

class _KFPageState extends State<KFPage> {
  List kflist = List();
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: Text('我的客服',
              style: TextStyle(
                  fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                  color: Colors.black)),
          backgroundColor: Colors.white,
          brightness: Brightness.light,
          centerTitle: true,
          iconTheme: new IconThemeData(color: Colors.black),
          elevation: 0.5,
        ),
        body: Container(
            alignment: Alignment.center,
            height: kScreenHeight,
            width: ScreenUtil.getInstance().setWidth(750),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage("images/service_bg.png"),
                fit: BoxFit.cover,
              ),
            ),
            child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.all(kMargin60),
                child: Column(children: <Widget>[
                  Container(height: ScreenUtil.getInstance().setHeight(480)),
                  ClipOval(
                      child: Image.asset(
                    'images/logo.png',
                    fit: BoxFit.cover,
                    width: ScreenUtil.getInstance().setWidth(DP_160),
                    height: ScreenUtil.getInstance().setWidth(DP_160),
                  )),
                  Container(height: ScreenUtil.getInstance().setHeight(50)),
                  // Container(
                  //   alignment: Alignment.topLeft,
                  //   margin: EdgeInsets.only(bottom: kMargin30),
                  //   child: Text(
                  //     '日尚臻选',
                  //     style: TextStyle(
                  //         color: white,
                  //         fontWeight: FontWeight.w600,
                  //         fontSize: ScreenUtil.getInstance()
                  //             .setSp(BIG_FONT_SIZE * 1.5)),
                  //   ),
                  // ),
                  // Container(
                  //   alignment: Alignment.topLeft,
                  //   margin: EdgeInsets.only(bottom: kMargin30),
                  //   child: Text(
                  //     '如您有任何问题，可联系我们的客服',
                  //     style: TextStyle(
                  //         color: white,
                  //         fontWeight: FontWeight.w300,
                  //         fontSize:
                  //             ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  //   ),
                  // ),
                  // Container(
                  //   alignment: Alignment.topLeft,
                  //   margin: EdgeInsets.only(bottom: kMargin30),
                  //   child: Text(
                  //     '联系方式如下：',
                  //     style: TextStyle(
                  //         color: white,
                  //         fontWeight: FontWeight.w300,
                  //         fontSize:
                  //             ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  //   ),
                  // ),
                  Container(
                    alignment: Alignment.center,
                    child: ListView.builder(
                      shrinkWrap: true,
                      itemBuilder: (BuildContext context, int index) {
                        return _buildListItem(context, index);
                      },
                      scrollDirection: Axis.vertical,
                      itemCount: kflist.length,
                    ),
                  )
                ]))));
  }

  Widget _buildListItem(BuildContext context, int index) {
    return Container(
        alignment: Alignment.center,
        width: ScreenUtil.getInstance().setWidth(750),
        child: Row(
          children: <Widget>[
            Container(
              alignment: Alignment.center,
              child: RichText(
                  textAlign: TextAlign.left,
                  text: TextSpan(
                      style: TextStyle(
                          color: Colors.black38,
                          fontSize:
                              ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                      children: [
                        TextSpan(
                            text: (kflist[index]['type'].toString() == '1'
                                    ? 'QQ'
                                    : (kflist[index]['type'].toString() == '2'
                                        ? '微信'
                                        : '旺旺')) +
                                '：'),
                        TextSpan(
                            text: kflist[index]['num'].toString(),
                            style: TextStyle(
                                color: Colors.red,
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(BIG_FONT_SIZE),
                                    fontWeight: FontWeight.w600)),
                      ])),
            ),
            GestureDetector(
              onTap: (){
                Clipboard.setData(ClipboardData(text:kflist[index]['num'].toString()));
                Toast.toast(context,msg:'复制成功');
              },
              child:  Container(
              margin: EdgeInsets.all(ScreenUtil.getInstance().setWidth(10)),
              alignment: Alignment.center,
              width: ScreenUtil.getInstance().setWidth(80),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(4.0),
               
              ),
              child: Text('复制'),
            )
            )
           
          ],
        ));
  }

  @override
  void initState() {
    super.initState();
    getKefu();
  }

  getKefu() async {
    // Map map = new Map();
    //  FormData format=FormData.fromMap(new Map<String, dynamic>.from(map));
    // map['sign_type'] = createSignType(Api.URL_KFList);
    Response<String> response = await HttpUtil().get(Api.URL_KFList);
    print(Api.URL_KFList);
    Map userMap = json.decode(response.data);
    print(userMap);
    if (userMap['code'] == 1) {
      if (mounted) {
        setState(() {
          kflist = userMap['data']['list'];
        });
      }
    }
  }
}
